package com.xbongbong.saas.script.tempenum.product;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.enums.DefaultTypeEnum;
import com.xbongbong.paas.field.DefaultAttrPoJo;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.FormulaPoJo;
import com.xbongbong.paas.field.LinkedAttrPoJo;
import com.xbongbong.paas.field.NumericalLimitsPoJo;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.pro.enums.AmountFlagEnum;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.SummaryTypeEnum;
import com.xbongbong.paas.field.LinkInfoPojo;
import com.xbongbong.paas.field.LinkKeyFieldInfoPojo;
import com.xbongbong.saas.enums.ForbiddenSettingEnum;
import com.xbongbong.saas.enums.SelectProductEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.RefundEnum;
import com.xbongbong.saas.enums.product.RefundProductEnum;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author haibin.zhang
 * @version v1.0
 * @date 2019/10/12 10:32
 * @since v1.0
 */
@Getter
public enum TempRefundProductEnum {
    PRODUCT(RefundProductEnum.PRODUCT),
    UNIT(RefundProductEnum.UNIT),
    NUM(RefundProductEnum.NUM),
    PRICE(RefundProductEnum.PRICE),
    SUBTOTAL(RefundProductEnum.SUBTOTAL),
    MEMO(RefundProductEnum.MEMO),
    COST(RefundProductEnum.COST),
    SOURCE_DOC(RefundProductEnum.SOURCE_DOC),
    INSTOCK_NUM(RefundProductEnum.INSTOCK_NUM),
    WAIT_INSTOCK_NUM(RefundProductEnum.WAIT_INSTOCK_NUM),
    BATCH(RefundProductEnum.BATCH),
    PRODUCE_DATE(RefundProductEnum.PRODUCE_DATE),
    GUARANTEE_PERIOD(RefundProductEnum.GUARANTEE_PERIOD),
    EXPIRE_DATE(RefundProductEnum.EXPIRE_DATE),
    REFUND_STATUS(RefundProductEnum.REFUND_STATUS),
    ;

    private RefundProductEnum refundProductEnum;

    TempRefundProductEnum(RefundProductEnum refundProductEnum) {
        this.refundProductEnum = refundProductEnum;
    }

    public static void initProduct(FieldAttrEntity fieldAttrEntity, List<LinkedAttrPoJo> linkedAttrPoJoList,List<LinkedAttrPoJo> linkedShowPoJoList, PaasFormEntityExt paasFormEntity){
        LinkInfoPojo linkInfoPojo = new LinkInfoPojo();
        linkInfoPojo.setLinkBusinessType(XbbRefTypeEnum.PRODUCT.getCode());
        linkInfoPojo.setLinkedAttr(linkedAttrPoJoList);
        linkInfoPojo.setLinkSaasMark(SaasMarkEnum.SAAS.getCode());
        linkInfoPojo.setLinkKey(linkedAttrPoJoList.get(0).getAttr());
        linkInfoPojo.setLinkFormId(paasFormEntity.getId());
        linkInfoPojo.setLinkMenuId(paasFormEntity.getMenuId());
        linkInfoPojo.setLinkAppId(paasFormEntity.getAppId());
        linkInfoPojo.setCondition(new ArrayList<>());
        LinkKeyFieldInfoPojo linkKeyFieldInfoPojo = new LinkKeyFieldInfoPojo();
        BeanUtil.copyProperties(linkedAttrPoJoList.get(0),linkKeyFieldInfoPojo);
        linkInfoPojo.setLinkKeyAttr(linkKeyFieldInfoPojo);
        linkInfoPojo.setLinkedShowAttr(linkedShowPoJoList);
        fieldAttrEntity.setLinkInfo(linkInfoPojo);
        fieldAttrEntity.setShowEditable(1);
        fieldAttrEntity.setForbiddenSettingList(Arrays.asList(ForbiddenSettingEnum.DISABLE_VISIBLE_PERMISSION.getAlias(),ForbiddenSettingEnum.DISABLE_LINK_FORM_LINK_FIELD.getAlias()));
    }
    public static void initCost(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setIsForbidden(0);
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.SUM.getType());
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
//        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
//        FormulaPoJo formulaInfo = new FormulaPoJo();
//        String formula = "{self." + RefundEnum.PRODUCTS.getAttr() + "." + SelectProductEnum.PRICE.getAttr() + "}*{self." + RefundEnum.PRODUCTS.getAttr() + "." + SelectProductEnum.NUM.getAttr() + "}";
//        JSONObject labelMap = new JSONObject();
//        labelMap.put("self." + RefundEnum.PRODUCTS.getAttr() + "." + SelectProductEnum.PRICE.getAttr(), "关联产品.单价");
//        labelMap.put("self." + RefundEnum.PRODUCTS.getAttr() + "." + SelectProductEnum.NUM.getAttr(), "关联产品.数量");
//        formulaInfo.setFormula(formula);
//        formulaInfo.setLabelMap(labelMap);
//        defaultAttrPoJo.setDefaultType("formula");
//        defaultAttrPoJo.setFormulaInfo(formulaInfo);
//        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
        // 合同金额默认限定 0 ~ 100000000000
        NumericalLimitsPoJo numericalLimits = new NumericalLimitsPoJo();
        numericalLimits.setMin(0D);
        numericalLimits.setMax(100000000000D);
        fieldAttrEntity.setNumericalLimits(numericalLimits);
        fieldAttrEntity.setNumericalLimitsFlag(1);
        fieldAttrEntity.setNumAccuracyAndScopeHide(1);
        List<String> settingList = Arrays.asList(ForbiddenSettingEnum.DISABLE_DEFAULT.getAlias(), ForbiddenSettingEnum.DISABLE_SUMMARY.getAlias(),ForbiddenSettingEnum.DISABLE_MEMO.getAlias(),ForbiddenSettingEnum.DISABLE_MONEY_TYPE.getAlias(),ForbiddenSettingEnum.DISABLE_VISIBLE_PERMISSION.getAlias());
        fieldAttrEntity.setForbiddenSettingList(settingList);
    }
    // 初始化产品中的其他属性
    public static void initPrice(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setShowEditable(1);
        fieldAttrEntity.setNumAccuracyAndScopeHide(1);
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
        List<String> settingList = Arrays.asList(ForbiddenSettingEnum.DISABLE_DEFAULT.getAlias(), ForbiddenSettingEnum.DISABLE_MONEY_TYPE.getAlias());
        fieldAttrEntity.setForbiddenSettingList(settingList);
    }

    public static void initNum(FieldAttrEntity fieldAttrEntity){
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        defaultAttrPoJo.setDefaultValue(1);
        defaultAttrPoJo.setDefaultType(DefaultTypeEnum.CUSTOM.getAlias());
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
        fieldAttrEntity.setIsForbidden(1);
        fieldAttrEntity.setNumAccuracyAndScopeHide(1);
        fieldAttrEntity.setShowEditable(1);
        fieldAttrEntity.setForbiddenSettingList(Collections.singletonList(ForbiddenSettingEnum.DISABLE_MONEY_TYPE.getAlias()));
    }

    public static void initSubTotal(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.SUM.getType());
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        FormulaPoJo formulaInfo = new FormulaPoJo();
        String formula = "{self." + RefundEnum.PRODUCTS.getAttr() + "." + SelectProductEnum.PRICE.getAttr() + "}*{self." + RefundEnum.PRODUCTS.getAttr() + "." + SelectProductEnum.NUM.getAttr() + "}";
        JSONObject labelMap = new JSONObject();
        labelMap.put("self." + RefundEnum.PRODUCTS.getAttr() + "." + SelectProductEnum.PRICE.getAttr(), "关联产品.单价");
        labelMap.put("self." + RefundEnum.PRODUCTS.getAttr() + "." + SelectProductEnum.NUM.getAttr(), "关联产品.数量");
        formulaInfo.setFormula(formula);
        formulaInfo.setLabelMap(labelMap);
        defaultAttrPoJo.setDefaultType("formula");
        defaultAttrPoJo.setFormulaInfo(formulaInfo);
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
        // 合同金额默认限定 0 ~ 100000000000
        NumericalLimitsPoJo numericalLimits = new NumericalLimitsPoJo();
        numericalLimits.setMin(0D);
        numericalLimits.setMax(100000000000D);
        fieldAttrEntity.setNumericalLimits(numericalLimits);
        fieldAttrEntity.setNumericalLimitsFlag(1);
        fieldAttrEntity.setNumAccuracyAndScopeHide(1);
        List<String> settingList = Arrays.asList(ForbiddenSettingEnum.DISABLE_DEFAULT.getAlias(), ForbiddenSettingEnum.DISABLE_MONEY_TYPE.getAlias());
        fieldAttrEntity.setForbiddenSettingList(settingList);
    }

    public static void initUnit(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setEditable(0);
        List<String> settingList = Arrays.asList(ForbiddenSettingEnum.DISABLE_ITEMS.getAlias(),
                ForbiddenSettingEnum.DISABLE_RULE.getAlias());
        fieldAttrEntity.setForbiddenSettingList(settingList);
    }

    public static void initSourceDoc(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setEditable(0);
        List<String> settingList = Arrays.asList(ForbiddenSettingEnum.DISABLE_DEFAULT.getAlias(), ForbiddenSettingEnum.DISABLE_TYPE.getAlias());
        fieldAttrEntity.setForbiddenSettingList(settingList);
    }

    public static void initMemo(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setShowEditable(1);
    }

    public static void initInNum(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setShowEditable(0);
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.NONE.getType());
        List<String> settingList = Arrays.asList(ForbiddenSettingEnum.DISABLE_ITEMS.getAlias(),ForbiddenSettingEnum.DISABLE_MEMO.getAlias(), ForbiddenSettingEnum.DISABLE_DEFAULT.getAlias(),ForbiddenSettingEnum.DISABLE_TYPE.getAlias(),ForbiddenSettingEnum.DISABLE_MONEY_TYPE.getAlias(),ForbiddenSettingEnum.DISABLE_RULE.getAlias(),ForbiddenSettingEnum.DISABLE_SUMMARY.getAlias());
        fieldAttrEntity.setForbiddenSettingList(settingList);
    }
    public static void initWaitInNum(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setShowEditable(0);
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.NONE.getType());
        List<String> settingList = Arrays.asList(ForbiddenSettingEnum.DISABLE_ITEMS.getAlias(),ForbiddenSettingEnum.DISABLE_MEMO.getAlias(), ForbiddenSettingEnum.DISABLE_DEFAULT.getAlias(),ForbiddenSettingEnum.DISABLE_TYPE.getAlias(),ForbiddenSettingEnum.DISABLE_MONEY_TYPE.getAlias(),ForbiddenSettingEnum.DISABLE_RULE.getAlias(),ForbiddenSettingEnum.DISABLE_SUMMARY.getAlias());
        fieldAttrEntity.setForbiddenSettingList(settingList);
    }

    public static void initProductDate(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setShowEditable(0);
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setDateType("yyyy-MM-dd");
        List<String> settingList = Arrays.asList(ForbiddenSettingEnum.DISABLE_DEFAULT.getAlias(),
        ForbiddenSettingEnum.DISABLE_RULE.getAlias(), ForbiddenSettingEnum.DISABLE_REQUIRE_RULE.getAlias());
        fieldAttrEntity.setForbiddenSettingList(settingList);
    }

    public static void initBatch(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setShowEditable(0);
        fieldAttrEntity.setEditable(0);
        List<String> settingList = Arrays.asList(ForbiddenSettingEnum.DISABLE_DEFAULT.getAlias(),
        ForbiddenSettingEnum.DISABLE_RULE.getAlias(), ForbiddenSettingEnum.DISABLE_TYPE.getAlias(), ForbiddenSettingEnum.DISABLE_REQUIRE_RULE.getAlias());
        fieldAttrEntity.setForbiddenSettingList(settingList);
    }

    public static void initGuaranteePeriod(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setIntegerOnly(1);
        fieldAttrEntity.setShowEditable(0);
        fieldAttrEntity.setEditable(0);
        List<String> settingList = Arrays.asList(ForbiddenSettingEnum.DISABLE_MONEY_TYPE.getAlias(),ForbiddenSettingEnum.DISABLE_DEFAULT.getAlias(), ForbiddenSettingEnum.DISABLE_RULE.getAlias(), ForbiddenSettingEnum.DISABLE_REQUIRE_RULE.getAlias());
        fieldAttrEntity.setForbiddenSettingList(settingList);
    }

    public static void initDetailStatus(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setForbiddenSettingList(Arrays.asList(ForbiddenSettingEnum.DISABLE_TYPE.getAlias(),ForbiddenSettingEnum.DISABLE_DEFAULT.getAlias()));
    }

    public static void initExpireDate(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setShowEditable(0);
        fieldAttrEntity.setDateType("yyyy-MM-dd");
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        FormulaPoJo formulaInfo = new FormulaPoJo();
        String formula = "{self." + RefundEnum.PRODUCTS.getAttr() + "." + SelectProductEnum.PRODUCE_DATE.getAttr() + "}+24*60*60*{self." + RefundEnum.PRODUCTS.getAttr() + "." + SelectProductEnum.GUARANTEE_PERIOD.getAttr() + "}";
        JSONObject labelMap = new JSONObject();
        labelMap.put("self." + RefundEnum.PRODUCTS.getAttr() + "." + SelectProductEnum.PRODUCE_DATE.getAttr(), "关联产品.生产日期");
        labelMap.put("self." + RefundEnum.PRODUCTS.getAttr() + "." + SelectProductEnum.GUARANTEE_PERIOD.getAttr(), "关联产品.保质期");
        formulaInfo.setFormula(formula);
        formulaInfo.setLabelMap(labelMap);
        defaultAttrPoJo.setDefaultType("formula");
        defaultAttrPoJo.setFormulaInfo(formulaInfo);
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
    }


}
